#-------------------------------------------------------------------------------
# Name:        AMOS Image Name Fetcher
# Purpose:
#
# Author:      bc6
#
# Created:     10/02/2012
# Copyright:   (c) bc6 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import urllib
import xml.dom.minidom
import time
import string

def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)

def genURLs(urls):
    years = [2009, 2010]
    months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

    for year in years:
        for month in months:
            cur_url = "http://amosweb.cse.wustl.edu/find_nearby_images?camera_id=984&year=%s&month=%s&day=1&hour=12&minute=0&second=0" %(year, month)
            urls.append(cur_url)

def run():

    urls = []
    img_names = []

    genURLs(urls) # we're generating urls for our url fetch

    for each in urls:
        fetched = urllib.urlopen(each)
        xmldom = xml.dom.minidom.parse(fetched)
        fetched.close()

        timestamps = xmldom.getElementsByTagName("timestamps")[0].getElementsByTagName("timestamp")
        for ts in timestamps:
            name = getText(ts.childNodes)
            if name not in img_names:
                # we're doing some filtering of images...
                # we're discarding the image if it's not between the one hour window we want
                right_str = string.split(name, "_")[1]
                hour_str = right_str[:2]
                hour = int(hour_str)
                if (hour < 15) and (hour >= 14):
                    img_names.append(name)
    import random
    random.shuffle(img_names)
    with open("names.txt", "w") as f:
        for i in xrange(len(img_names)/4):
            f.write("%s %s %s %s\n" % (img_names[i*4], img_names[i*4+1], img_names[i*4+2], img_names[i*4+3]))
    f.close()

    print "done fetching... added %s names" % len(img_names)



def main():
    run()

if __name__ == '__main__':
    main()
